from datetime import datetime, date, time
from math import ceil

NUMBER_WIDTH = 5

def calender(year: int = date.today().year, columns: int = 1) -> str:
    calender_strings = [[]]
    current_date = date(year, 1, 1)
    week = [None] * 7
    result = ""

    def update_week():
        nonlocal week
        if week != [None] * 7:
            week_string = ""
            for day in week:
                week_string += "{:{:d}d}".format(day, NUMBER_WIDTH) if day else " " * NUMBER_WIDTH
            calender_strings[-1].append(week_string)
        week = [None] * 7

    while True:
        if current_date.month > len(calender_strings):
            update_week()
            calender_strings.append([])
        week[current_date.isoweekday() % 7] = current_date.day
        if week[-1]:
            update_week()
        current_date = date.fromordinal(current_date.toordinal() + 1)
        if current_date.year > year:
            update_week()
            break
    
    result += "=" * (columns * (7 * NUMBER_WIDTH + 1) - 1) + "\n"
    result += "{:=^{:d}d}\n".format(year, columns * (7 * NUMBER_WIDTH + 1) - 1)
    result += "=" * (columns * (7 * NUMBER_WIDTH + 1) - 1) + "\n"

    for row in range(ceil(12 / columns)):
        current_calender = calender_strings[columns * row : columns * (row + 1) if columns * (row + 1) < 13 else 12]
        length = len(current_calender)
        result += "\n"
        result += (("=" * (7 * NUMBER_WIDTH) + " ") * length).rstrip() + "\n"
        for i in range(length):
            result += "{:=^{:d}s}".format("{:02d}".format(row * columns + i + 1), (7 * NUMBER_WIDTH)) + " "
        result = result.rstrip() + "\n"
        result += (("=" * (7 * NUMBER_WIDTH) + " ") * length).rstrip() + "\n"
        result += (" Sun. Mon. Tue. Wed. Thu. Fri. Sat. " * length).rstrip() + "\n"
        result += (("=" * (7 * NUMBER_WIDTH) + " ") * length).rstrip() + "\n"
        length = max([len(month) for month in current_calender])
        for month_row in range(length):
            for month in current_calender:
                if month_row < len(month):
                    result += month[month_row] + " "
                else:
                    result += " " * (7 * NUMBER_WIDTH + 1)
            result = result.rstrip() + "\n"

    return result
        
if __name__ == "__main__":
    print(calender())